<?php
/**
 * Created by PhpStorm.
 * User: mojie126
 * Date: 2017/7/5 0005
 * Time: 上午 11:16
 */

namespace app\index\controller;

use app\common\imdb;
use app\index\model\Users;
use think\Validate;

class EditController extends Base
{
	public function index($id, $type)
	{
		$this->assign("title", "编辑");
		$db = getTorrentDB($type);
		$torrent = db($db)->where("id", $id)->find();
		$this->assign("torrent", $torrent);
		$this->assign("user", Users::getUserInfo(session("uid")));
		if ($torrent['uploader'] != session("uid")) {
			$this->checkAccess();
		}
		$this->getAccess();

		return $this->fetch("Details/Edit");
	}

	public function getCategory($id, $type)
	{
		$db = getTorrentDB($type);
		$res = db($db)->where("id", $id)->find();
		unset($res['bininfohash']);//切记从数组中去除，因为其值是二进制
		$res['t_olcate'] = db("category")->where(["display" => ["=", "0"], "pid" => ["=", "0"]])->order("sort", "DESC")->select();
		$res['t_slcate'] = db("category")->where(["display" => ["=", "0"], "pid" => ["=", $res['olcate']]])->order("sort", "DESC")->select();
		$res['t_author'] = db("information")->select();
		$res['t_container'] = db("container")->select();
		$res['t_resolution'] = db("resolution")->select();
		$res['t_source'] = db("source")->select();
		if ($res) {
			jsonReturn(1, "", $res);
		} else {
			jsonReturn();
		}
	}

	public function doEdit()
	{
		$info = input("param.");

		//输入验证
		$rule = [
			"olcate|分类" => "require|notIn:0",
			"slcate|二级分类" => "require|notIn:0",
			"maintitle|主标题" => "require",
			"descs|详情" => "require",
			"__token__" => "require|token"
		];
		$msg = [
			"__token__" => "请勿重复提交"
		];
		$data = [
			"olcate" => $info['olcate'],
			"slcate" => $info['slcate'],
			"maintitle" => $info['maintitle'],
			"descs" => $info['descs'],
			"__token__" => $info['__token__']
		];
		$validate = new Validate($rule, $msg);
		$check = $validate->check($data);
		if (!$check) {
			$this->error($validate->getError());
		}
		//输入验证

		$id = $info['torrentid'];
		$type = $info['torrenttype'];
		$db = getTorrentDB($type);
		$torrent = db($db)->where("id", $id)->find();
		if ($torrent) {
			$imdburl = !empty($info['imdburl']) ? $info['imdburl'] : $torrent['imdburl'];
			$doubanurl = !empty($info['doubanurl']) ? $info['doubanurl'] : $torrent['doubanurl'];
			if ($imdburl == $torrent['imdburl']) {
				$imdbrating = $torrent['imdbscore'];
			} else {
				$imdbrating = imdb::getIMDbRating($info['imdburl']);
			}
			$sticktime = "";
			$discounttime = "";
			if ($this->checkAccess(1)) {
				if (!empty($info['sticklevel']) && isset($info['sticktime'])) {
					if ($info['sticktime'] == "") {
						$sticktime = $torrent['sticktime'];
					} elseif ($info['sticktime'] == 0) {
						$sticktime = "";
					} else {
						$sticktime = date("Y-m-d H:i:s", time() + $info['sticktime'] * 3600);
					}
				}
				if (!empty($info['discount']) && isset($info['discounttime'])) {
					if ($info['discounttime'] == "") {
						$discounttime = $torrent['discounttime'];
					} elseif ($info['discounttime'] == 0) {
						$discounttime = "";
					} else {
						$discounttime = date("Y-m-d H:i:s", time() + $info['discounttime'] * 3600);
					}
				}
			}
			$update = [
				"maintitle" => $info['maintitle'],
				"subtitle" => $info['subtitle'],
				"descs" => $info['descs'],
				"olcate" => $info['olcate'],
				"slcate" => empty($info['slcate']) ? $torrent['slcate'] : $info['slcate'],
				"doubanurl" => $doubanurl,
				"imdburl" => $imdburl,
				"imdbscore" => $imdbrating,
				"official" => isset($info['official']) ? $info['official'] : 0,
				"excl" => isset($info['excl']) ? $info['excl'] : 0,
				"first" => isset($info['first']) ? $info['first'] : 0,
				"hr" => isset($info['hr']) ? $info['hr'] : 0,
				"anonymous" => isset($info['anonymous']) ? $info['anonymous'] : 0,
				"author" => $info['author'],
				"sticklevel" => $info['sticklevel'],
				"sticktime" => $sticktime,
				"discount" => $info['discount'],
				"discounttime" => $discounttime
			];
			if ($this->checkAccess(1)) {
				$update = array_merge($update, [
					"official" => isset($info['official']) ? $info['official'] : 0,
					"excl" => isset($info['excl']) ? $info['excl'] : 0,
					"first" => isset($info['first']) ? $info['first'] : 0,
					"hr" => isset($info['hr']) ? $info['hr'] : 0
				]);
			}
			if ($info['olcate'] == 3) {
				$update = array_merge($update, ["logscore" => isset($info['logscore']) ? $info['logscore'] : 0]);
			}
			if ($info['olcate'] != 1) {
				//存入series表
				//输入验证
				$rule = [
					"subtitle|季集名称" => "require",
					"albumname|聚合名称" => "require",
					"container|容器类型" => "require",
					"source|获取源类型" => "require",
					"resolution|分辨率" => "require"
				];
				$data = [
					"subtitle" => $info['subtitle'],
					"albumname" => $info['albumname'],
					"container" => $info['container'],
					"source" => $info['source'],
					"resolution" => $info['resolution']
				];
				$validate = new Validate($rule);
				$check = $validate->check($data);
				if (!$check) {
					$this->error($validate->getError(), "/edit/$id/" . substr($db, 0, 1));
				}
				//输入验证
				$update = array_merge($update, [
					"albumname" => preg_replace("/[\/\\\]/", "", $info['albumname']),
					"container" => $info['container'],
					"source" => $info['source'],
					"resolution" => $info['resolution']
					//其他需要存的项目
				]);
				db($db)->where("id", $id)->update($update);
				$this->success("修改成功", "/$db/id/$id");
			} else {
				db($db)->where("id", $id)->update($update);
				$this->success("修改成功", "/$db/id/$id");
			}
		} else {
			$this->error("修改失败");
		}
	}
}